package com.itmiaojie.utils;

import com.itmiaojie.pojo.Article;

import com.itmiaojie.pojo.vo.HotArticleVo;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

public class BeanCopyUtils {
    public BeanCopyUtils(){
    }

    public static <V> V copyBean( Object source,Class<V> clazz){
        V result = null;
        try {
          result = clazz.newInstance();
            BeanUtils.copyProperties(source,result);
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static <O,V> List<V> copyBeanList(List<O> list,Class<V> v) throws IllegalAccessException, InstantiationException {
        List<V> result = new ArrayList<>() ;
        for (O o : list) {
            V res = copyBean(o, v);
            result.add(res);
        }
        return result;
    }

    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        Article article = new Article();
        article.setId(1L);
        article.setTitle("test");
        article.setViewCount(12L);
        HotArticleVo hotArticle = copyBean(article, HotArticleVo.class);
        System.out.println(hotArticle);
        List<Article> list =new ArrayList<>();
        list.add(article);
        List<HotArticleVo> list1 = copyBeanList(list, HotArticleVo.class);
        System.out.println(list1);
    }
}
